package org.aom.litebean.module.security;

import org.aom.entity.security.Authority;
import org.aom.litebean.module.base.BaseGridCrudBean;
import org.aom.service.security.IAuthorityManager;
import org.aom.util.ManageBeanUtil;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.ManagedProperty;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.component.grid.provider.GridDataProvider;

@ManagedBean(name = "module.security.authorityListBean", scope = ManagedBeanScope.REQUEST)
public class AuthorityListBean extends BaseGridCrudBean<Authority, Long> {

	@ManagedProperty("#{authorityManager}")
	private IAuthorityManager authorityManager;

	@Bind(id = "authorityDataGrid", attribute = "idColumn")
	private static String idColumn = "id";

	@Bind(id = "authorityDataGrid", attribute = "binding")
	private UIDataGrid authorityDataGrid;

	@Override
	protected Class<Authority> getEntityClass() {
		return Authority.class;
	}

	@Override
	protected IAuthorityManager getEntityManager() {
		return this.authorityManager;
	}

	@Override
	protected String getIdColumn() {
		return idColumn;
	}

	@Bind(id = "authorityDataGrid", attribute = "dataProvider")
	public GridDataProvider getDataProvider() {
		return super.getDataProvider();
	}

	@BeforeRender
	public void beforeRender(boolean isPostBack) {
		initBaseResource();
	}

	@Override
	@Action(id = "add", event = "onclick")
	protected void processToAdd() {
		String url = "authorityForm.faces";
		ManageBeanUtil.openDialog(null, "add", OperationFrame, url,
				OperationDialog, 500, 150);
	}

	@Override
	@Action(id = "authorityDataGrid", event = "ondblclick")
	protected void processToEdit() {
		try {
			Authority auth = (Authority) getSelectEntity();
			String url = "authorityForm.faces";
			ManageBeanUtil.openDialog(auth.getId(), "mod", OperationFrame, url,
					OperationDialog, 500, 150);
		} catch (ArrayIndexOutOfBoundsException e) {
			ManageBeanUtil.noSelectWarn();
		}
	}

	@Override
	protected void processToSearch() {
	}

	@Override
	protected void initAdd() {
	}

	@Override
	protected UIDataGrid getDataGrid() {
		return authorityDataGrid;
	}

	@Override
	protected void delete() {
		try {
			Authority entity = (Authority) getSelectEntity();
			authorityManager.delete(entity.getId());
			resetGrid();
		} catch (ArrayIndexOutOfBoundsException e) {
			ManageBeanUtil.noSelectWarn();
		}
	}
}
